package com.mayisoftware.mayioa.business.worktime.impl.controller;

import com.mayisoftware.mayioa.business.statistic.api.domain.EChartsBarVO;
import com.mayisoftware.mayioa.business.statistic.api.domain.SeriseData;
import com.mayisoftware.mayioa.business.worktime.api.domain.*;
import com.mayisoftware.mayioa.business.worktime.api.domain.board.TMBoardResult;
import com.mayisoftware.mayioa.business.worktime.api.service.IWorkTimeBoardService;
import com.mayisoftware.mayioa.business.worktime.api.service.IWorkTimeDataService;
import com.mayisoftware.mayioa.common.api.annotation.Log;
import com.mayisoftware.mayioa.common.api.base.AjaxResult;
import com.mayisoftware.mayioa.common.api.enums.BusinessType;
import com.mayisoftware.mayioa.common.impl.utils.ExcelUtil;
import com.mayisoftware.mayioa.common.impl.utils.StringUtils;
import com.mayisoftware.mayioa.platform.impl.web.base.BaseController;
import com.mayisoftware.mayioa.platform.impl.web.page.TableDataInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/worktime/data")
public class WorkTimeDataController extends BaseController{
    private static final Logger log = LoggerFactory.getLogger(WorkTimeDataController.class);

    @Autowired
    private IWorkTimeDataService dataService;

    private String prefix = "worktime/data";

    @RequiresPermissions("worktime:data:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/worktimedata";
    }


    @RequiresPermissions("worktime:data:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WrokTimeImportInfo record)
    {
        startPage();
        TMBoardResult result = new TMBoardResult();
        return getDataTable(result.getLess8Hours());
    }


    @RequiresPermissions("worktime:data:list")
    @PostMapping("/list/queryExtraGreater8Hours")
    @ResponseBody
    public TableDataInfo queryExtraGreater8Hours(WrokTimeImportInfo record)
    {
        dataService.convertToLabel(record);
        startPage();
        List<TMWorktimeDetailFlow> flows = dataService.queryExtraGreater8Hours(record);
        return getDataTable(flows);
    }

    /**
     * 欠工时存在加班的情况
     * @param record
     * @return
     */
    @RequiresPermissions("worktime:data:list")
    @PostMapping("/list/queryLackHasExtra")
    @ResponseBody
    public TableDataInfo queryLackHasExtra(WrokTimeImportInfo record)
    {
        dataService.convertToLabel(record);
        startPage();
        List<TMWorktimeMonthInfo> monthInfos = dataService.queryLackHasExtra(record);
        return getDataTable(monthInfos);
    }

    /**
     * 欠工时存在加班的情况
     * @param record
     * @return
     */
    @RequiresPermissions("worktime:data:list")
    @PostMapping("/list/queryLackWorktime")
    @ResponseBody
    public TableDataInfo queryLackWorktime(WrokTimeImportInfo record)
    {
        dataService.convertToLabel(record);
        startPage();
        List<TMWorktimeMonthInfo> monthInfos = dataService.queryLackWorktime(record);
        return getDataTable(monthInfos);
    }
    /**
     * 欠工时存在加班的情况
     * @param record
     * @return
     */
    @RequiresPermissions("worktime:data:list")
    @PostMapping("/list/queryExtraLess8Hours")
    @ResponseBody
    public TableDataInfo queryExtraLess8Hours(WrokTimeImportInfo record)
    {
        dataService.convertToLabel(record);
        startPage();
        StringBuilder sb = new StringBuilder();
        List<TMWorktimeDetailFlow> monthInfos = dataService.queryExtraLess8Hours(record);
        return getDataTable(monthInfos);
    }

    /**
     * 欠工时存在加班的情况
     * @param record
     * @return
     */
    @RequiresPermissions("worktime:data:list")
    @PostMapping("/list/queryHasDoubleFlows")
    @ResponseBody
    public TableDataInfo queryHasDoubleFlows(WrokTimeImportInfo record)
    {
        dataService.convertToLabel(record);
        startPage();
        List<TMWorktimeDetailFlow> monthInfos = dataService.queryHasDoubleFlows(record);
        return getDataTable(monthInfos);
    }

    /**
     * 欠工时存在加班的情况
     * @param record
     * @return
     */
    @RequiresPermissions("worktime:data:list")
    @PostMapping("/list/queryGreater3TimesLeave")
    @ResponseBody
    public TableDataInfo queryGreater3TimesLeave(WrokTimeImportInfo record)
    {
        dataService.convertToLabel(record);
        startPage();
        List<TMWorktimeLeaveFlow> monthInfos = dataService.queryGreater3TimesLeave(record);
        return getDataTable(monthInfos);
    }

    /**
     * 欠工时存在加班的情况
     * @param record
     * @return
     */
    @RequiresPermissions("worktime:data:list")
    @PostMapping("/list/queryDoubtRepairWorktime")
    @ResponseBody
    public TableDataInfo queryDoubtRepairWorktime(WrokTimeImportInfo record)
    {
        dataService.convertToLabel(record);
        startPage();
        List<TMWrokTimeFlow> worktimeFlows = dataService.queryDoubtRepairWorktime(record);
        return getDataTable(worktimeFlows);
    }

    /**
     * 欠工时存在加班的情况
     * @param record
     * @return
     */
    @RequiresPermissions("worktime:data:list")
    @PostMapping("/list/queryMonthOweWorktime")
    @ResponseBody
    public TableDataInfo queryMonthOweWorktime(WrokTimeImportInfo record) {
        dataService.convertToLabel(record);
        startPage();
        List<TMWrokTimeFlow> worktimeFlows = dataService.queryMonthOweWorktime(record);
        return getDataTable(worktimeFlows);
    }

    @Log(title = "导出工时信息", businessType = BusinessType.EXPORT)
    @RequiresPermissions("worktime:data:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WrokTimeImportInfo record)
    {
        List<TMWrokTimeFlow> worktimeFlows = dataService.queryMonthOweWorktime(record);
        ExcelUtil<TMWrokTimeFlow> util = new ExcelUtil<TMWrokTimeFlow>(TMWrokTimeFlow.class);
        return util.exportExcel(worktimeFlows, "工时异常信息");
    }


    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") WrokTimeImportInfo record, ModelMap mmap)
    {
        mmap.put("repairInfo", dataService.getRepairInfo(record));
        return prefix + "/edit";
    }
    /**
     * 修改HC信息
     */
    @RequiresPermissions("worktime:data:edit")
    @Log(title = "疑似打卡补充说明", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult editSave(DoubtRepairInfo repairInfo)
    {
        int records = 0;
        try{
            records = dataService.updateRepairInfo(repairInfo);

        }catch (Exception ex){
            return toAjax(0);
        }

        return  toAjax(records);

    }
}
